package cn.edu.tju.se.auth.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.tju.se.auth.repository.GroupRepository;
import cn.edu.tju.se.auth.domain.Group;
import cn.edu.tju.se.auth.domain.Role;
import cn.edu.tju.se.auth.domain.Udr;
import cn.edu.tju.se.auth.domain.User;
import cn.edu.tju.se.auth.service.GroupService;
import cn.edu.tju.se.auth.service.UdrService;

@Service
public class GroupServiceImpl implements GroupService {

	@Autowired GroupRepository groupRepository;
	@Autowired UdrService udrService;
	@Override
	public Group get(Long id) {
		return groupRepository.findOne(id);
	}

	@Override
	public List<Group> getAll() {
		return groupRepository.findAll();
	}


	@Override
	public Group add(Group group) {
		group.setMyUdr(new HashSet<Udr>());
		return groupRepository.save(group);
	}

	@Override
	public void update(Group group) {
		Group group1=this.get(group.getId());
		Set<Udr> udr=group1.getMyUdr();
		group.setMyUdr(udr);
		groupRepository.save(group);
	}

	@Override
	public void delete(Long id) {
		Group group1=this.get(id);
		Set<Udr> udr=group1.getMyUdr();
		udrService.deleteAll(udr,"group");
		groupRepository.delete(id);
	}

	@Override
	public Set<Udr> getMyUdr(Long id) {
		Group group=this.get(id);
		return group.getMyUdr();
	}
	
	
}
